<?php

/**

 * Created by PhpStorm.

 * User: Administrator

 * Date: 2018/11/19

 * Time: 16:51



 * 订单类

 */



namespace app\console\controller;

use app\common\trilateral\Classify as ClassifyAlias;

use think\Build;

use think\Controller;

use think\Db;

use think\Request;

use think\Url;

use app\interfaces\controller\Alipay;

use think\Exception;

use think\Session;

use app\console\model\RefundOrderAudit;

use think\Cache;

class Order extends Controller

{

	/**
	 *
	 *
	**/ 
	public function dingdan_ajax(){
		$msg = '';
		$param = Request::instance()->param();
        
		$type = $param['type'];
		if($type=='1'){
			$level = \session('role_id');//当前登录的管理员界别 5 7 8 9 10
			$pici_id =  $param['pici_id'];
			$order_id = $param['order_id'];
			//level=1是adminer  level-5 门店
			$level = $param['level'];
			$manage_id = $param['manage_id'];
		
		
		$list = Db::name('shop_assignor_records')
            ->where(['pici'=>$pici_id])
            ->where(['order_id'=>$order_id])
            ->order('id', 'desc')
            ->select(); 
			foreach ($list as $k => &$v){
				//assignor_id 人员
				if(!empty($v['assignor_id'])){
					$v['assignor_name'] = Db::name('shop_assignor')->where(['id'=>$v['assignor_id']])->value('name');
				}else if(empty($v['assignor_id'])){
					$v['assignor_name']='';
				}
				if($v['type'] == 1){
					$v['type_name'] = '服务提成';
				}elseif($v['type'] == 2){
					$v['type_name'] = '销售提成';
				}
				//1是商品 2是礼包 2是服务 3是礼包
				
				if($v['order_type'] == 1){
					$v['assignor_content'] = Db::name('shop_goods')->where('id',$v['single_id'])->value('title');
					$goods_id = Db::name('shop_goods')->where('id',$v['single_id'])->value('goods_id');
					$v['assignor_msg'] = Db::name('goods_type')->where('id',$goods_id)->value('title');
				}else if($v['order_type'] == 2){
				   $v['assignor_content']= Db::name('single')->where('id',$v['single_id'])->value('cat_name');
                   $v['assignor_msg']= Db::name('single')->where('id',$v['single_id'])->value('cat_name');
				}else if($v['order_type'] == 3){
				   $v['assignor_content']= Db::name('single')->where('id',$v['single_id'])->value('cat_name');
                   $v['assignor_msg']= Db::name('single')->where('id',$v['single_id'])->value('cat_name');
				}
				//提成金额 $v['assignor_price']
				$v['assignor_price'] = $v['assignor_price']."";
				//创建时间$v['create_time']
				//创建时间$v['id']
				//个数
				$v['count'] = Db::name('shop_assignor_records')->where(['pici'=>$pici_id])->where(['order_id'=>$order_id])->count(); 
				
				
				
				
				
				//$msg .=$v['assignor_name']."(".$v['type_name'].")".$v['assignor_msg']."==".$v['assignor_price']."==".$v['create_time']."==".$v['id']."==".$v['count'];
				$msg.="<tr class='service_people' id='ticheng'><td>服务人员提成(批次：".$v['pici'].")</td><td colspan='1'>".$v['assignor_name']."(".$v['type_name'].")"."</td><td rowspan='1'>".$v['assignor_msg']."</td><td colspan='1'>".$v['assignor_price']."元</td><td colspan='1'>".$v['create_time']."</td><td style=' border-bottom:1px solid #000'><label id='yincang_de'><input type='checkbox' t='0' value='".$v['id']."'>".$v['id']."</label></td></tr>";
			}
		$data = '{"res":"ok","massage":"'.$msg.'"}';
		//var_dump($list);	
		echo json_encode($data);
		}elseif($type=='2'){
			$level = \session('role_id');//当前登录的管理员界别 5 7 8 9 10
			$pici_id =  $param['pici_id'];
			$order_id = $param['order_id'];
			//level=1是adminer  level-5 门店
			$level = $param['level'];
			$manage_id = $param['manage_id'];
			 $is_edit = Db::name('shop_assignor_records')
				 ->where('pici', $pici_id)
				 ->where('order_id', $order_id)
				 ->update([
					'pici_status' =>2
				 ]);
				 
			//赋值一下	 
			if($level=='5'){
				$pici_list = Db::name('shop_assignor_records')->where('order_id',$order_id)->where('pici_status','1')->Distinct(true)->field('pici')->select();
				foreach($pici_list as $k => &$v){
					$v['shifou_tongyi'] = Db::name('shop_assignor_records')->where('order_id',$order_id)->where('pici',$v['pici'])->where('is_agree','1')->count();
					if($v['shifou_tongyi'] > '0'){
						$msg.="";
						$v['is_agree_agree'] = '0';
					}else{
						$msg.="<option value=".$v['pici'].">第".$v['pici']."批次</option>";
						$v['is_agree_agree']='1';
					}
				}
			}else{
				$pici_list = Db::name('shop_assignor_records')->where('order_id',$order_id)->Distinct(true)->field('pici')->select();
				foreach ($pici_list as $k => &$v){
					$msg.="<option value=".$v['pici'].">第".$v['pici']."批次</option>";
					$v['is_agree_agree']='1';
				}
			} 
		//$msg .= "<option value=''>请选择批次</option>".$msg;
				 
		$data = '{"res":"ok","massage":"'.$msg.'"}';
		//var_dump($list);	
		echo json_encode($data);		 
		}
	}


    /**

     * @return array|mixed|\think\response\View

     * 订单列表

     */

    public function index()

    {



        $param = Request::instance()->param();

        $level = \session('role_id');//当前登录的管理员界别 5 7 8 9 10



        $where = [];

        $auding_status = Session::get('auding_status');

        if($auding_status == 1){

            $where['o.shop_id'] = ['eq',\session('shop_id')];

            //$where['o.auding_status'] = ['eq',2];

        }

        $where['o.order_type'] = ['eq', 1];



        if (!empty($param['is_certification'])) $where['m.is_certification'] = ['eq', $param['is_certification']];

        if (!empty($param['status'])){

            if($param['status'] == 2 || $param['status'] == 6){

                $where['o.status'] = ['in', $this->status];

            }else{

                $where['o.status'] = ['eq', $param['status']];

            }

        }

        if (!empty($param['auding_status'])){

            $where['o.auding_status'] = ['eq', $param['auding_status']];

            $where['o.is_free_order'] = ['eq', 2];

        }

        if (!empty($param['finance_order'])) $where['o.finance_order'] = ['eq', $param['finance_order']];

        if (!empty($param['order_online'])) $where['o.order_online'] = ['eq', $param['order_online']];

        if (!empty($param['shop_id'])) $where['o.shop_id'] = ['eq', $param['shop_id']];

        if (!empty($param['title'])) $where['m.username|o.shop_order_number|m.car_number|og.title'] = ['like', '%'.$param['title'].'%'];

        if (!empty($param['interval_timer'])) {

            $interval_timer = explode('/', $param['interval_timer']);

            $where['o.create_time'] = ['between', [$interval_timer[0], $interval_timer[1]]];

        }

        if(session('role_id') != 1){

            $where['o.is_delete'] = ['eq', 1];

        }

//        dump($where);

        $data = \app\console\model\Order::alias('o')

            ->join('member m', 'o.member_id = m.id','left')

            ->join('shop s', 'o.shop_id = s.id','left')

            //->join('shop_assignor_records sar','o.id = sar.order_id','left')

//            ->join('shop_goods sg','sar.single_id = sg.id','left')

            ->join('order_goods og','og.order_id = o.id','left')

            ->field('o.device,o.is_delete,m.username,o.*,m.car_number,m.is_certification,s.shop_title,og.title')

            ->where($where)

            ->fetchSql(false)

            ->group('o.id')

            ->order('id','desc')

            ->paginate(10, false, ['query' =>$param])->each(function($item,$key) use($level){

                if(session('role_id') != 1){ //非超级管理员

                    $item->username = hideStr($item->username,1,6);

                }



                if($item->is_certification == 2 && !empty($item->car_number)){

                    //$item->car_number = '('.$item->car_number.')';

                }

                $goods_id_array = Db::name('order_goods')->where('order_id',$item->id)->column('goods_id');

                $cat_str = '';

                if(!empty($goods_id_array)){

                    foreach ($goods_id_array as $v){

                        $cat_str .= Db::name('shop_goods')->where('id',$v)->value('goods_title').'';

                    }

                }

                $item->cat_str = $cat_str;

                if($item['status'] == 12 || $item['status'] == 10 || $item['status'] == 11){

                    $item->refund_create_time = Db::name('order_refund')->where('order_id',$item['id'])->value('create_time');

                }

                $item->refund_button = false;

                $count = (new RefundOrderAudit())->where('order_id',$item->id)->where('status','11')->count();

                if($level == 5){

                    if($count == 0){

                        $item->refund_button = true;

                    }

                }

                if($level == 7){

                    if($count == 1){

                        $item->refund_button = true;

                    }

                }

                if($level == 8){

                    if($count == 2){

                        $item->refund_button = true;

                    }

                }

                if($level == 9){

                    if($count == 3){

                        $item->refund_button = true;

                    }

                }

                if($level == 10){

                    if($count == 4){

                        $item->refund_button = true;

                    }

                }

            });



        //echo \app\console\model\Order::getlastSql(); SELECT COUNT(*) AS tp_count FROM `sun_refund_order_audit` WHERE `order_id` = 941 AND `status` = 11 LIMIT 1

        $total_count = Db::name('order o')

            ->join('member m', 'o.member_id = m.id','left')

            ->join('shop s', 'o.shop_id = s.id')

           // ->join('shop_assignor_records sar','o.id = sar.order_id','left')

            ->join('order_goods og','og.order_id = o.id','left')

            ->field('o.*')

            ->group('o.id')

            ->where($where)->count();



        $total_price2 = Db::name('order o')

            ->join('member m', 'o.member_id = m.id','left')

            //->join('shop_assignor_records sar','o.id = sar.order_id','left')

            ->join('order_goods og','og.order_id = o.id','left')

            ->group('o.id')

            ->field('o.*,m.username,s.shop_title')



            ->where($where)->column('o.id');

        $total_price = 0;

        if(!empty($total_price2)){

            foreach ($total_price2 as $v){

                //$total_price = Db::name('order')->where('id','in',$total_price2)->sum('amount');

            }

        }

        $total_price = Db::name('order')->where('id','in',$total_price2)->sum('amount');



        //halt($total_price);

        $shop_list = Db::name('shop s')->field('id,shop_title')->select();



        $service_people_price = Db::name('shop_service_price')

            ->alias('ssp')

            ->join('single s','ssp.single_id = s.id','left')



            ->where('s.parent_id','gt',0)

            ->field('s.id,s.cat_name,ssp.person_price')

            ->select();











        $template = '';

        if(isset($param['table'])) $template = 'service_order_table';

        return view($template, ['data' => $data,



            'service_people_price' => $service_people_price,

            'shop_list' => $shop_list,

            'total_count' => $total_count,

            'total_price' => $total_price,

        ]);

    }



    /**

     * @param $id

     * @param Request $request

     * @return bool|int

     * 是否删除订单

     */

    public function is_delete($id, Request $request)

    {

        $id = $request->instance()->param('id');



        $status = Db::name('order')->where('id', $id)->value('is_delete') == '1' ? 2 : 1;



//        if (Db::name('order')->where('id', $id)->update(['is_delete' => $status])) {

//            return $status;

//        }

        if (Db::name('order')->where('id', $id)->delete()) {

            return $status;

        }

        return false;

    }



    /**

     * @param $id

     * 订单修改

     */
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    public function update($id)

    {


		//var_dump($_SESSION);
        $param = Request::instance()->param();

        $level = \session('role_id');//当前登录的管理员界别 5 7 8 9 10

        $find = Db::name('order')->find($id);



        $auding_status = Session::get('auding_status');

        if($auding_status == 2){

            Db::name('order o')->where('id',$id)->setField('is_read',1);

        }else{

            Db::name('order o')->where('id',$id)->setField('is_read_shop',1);

        }



        $refund = Db::name('order_refund ors')->field('ors.*,rr.title')->where('order_id',$find['id'])->join('refund_reason rr','ors.reason_id = rr.id')->find();

        if (Request::instance()->isPost()) {

            $car_number = Request::instance()->post('car_number');

            if(isset($car_number) && empty($car_number)){

                fail('必须填写车牌号');

            }



            if(!empty($param['assignor_id'])){



                if (count($param['assignor_id']) != count(array_unique($param['assignor_id']))) {

                    //fail('不能添加重复人员');

                }



                //$single_id = explode('-',$param['single_id'][0])[0];//门店服务分类 第一个类就代表所有相同的服务类了

               // $single_title = Db::name('single')->where('id',$single_id)->value('cat_name');//服务类型

               // $assignor_price = explode('-',$param['single_id'][0])[1];//门店金额

                for ($i = 0; $i < count($param['assignor_id']); $i++) {


					$content_a = '';
                    if($param['typea'] == 2){ //服务

                        $assignor_content = $param['assignor_content'][$i];

                    }else{

                        $assignor_content = $param['single_id'][$i];

                    }
					
					/****************************************/
							if($param['typea'][$i]=='1'){
								if($param['post_id'][$i] !=''){
									//查询岗位百分比
									$ticheng  = Db::name('goods_gangwei')->where('post_id', $param['post_id'][$i])->where('goods_id', $assignor_content)->order('id', 'desc')->limit('0,1')->value('post_fuwu');
								}else{
									$ticheng = '0';
								}
								$person_price1 = Db::name('goods_shop_price1')->where('shop_id', $find['shop_id'])->where('goods_id', $assignor_content)->count();
								if($person_price1=='0'){
									$person_price ='0'; 
									$price ='0'; 
								}else{
									$person_price = Db::name('goods_shop_price1')->where('shop_id', $find['shop_id'])->where('goods_id', $assignor_content)->value('price');  
									
								}
								//查询数量
								$number_s = Db::name('order_goods')->where('order_id', $find['id'])->where('goods_id',$assignor_content)->value('number');  
								//计算总提成
								$assignor_price = ($ticheng + $person_price)*$number_s;
								$param['assignor_price'][$i] = $assignor_price;
							}elseif($param['typea'][$i]=='2'){
								
								if($param['post_id'][$i] !=''){
									//查询岗位百分比
									$ticheng  = Db::name('goods_gangwei')->where('post_id', $param['post_id'][$i])->where('goods_id', $assignor_content)->order('id', 'desc')->limit('0,1')->value('post_xiaoshou');
								}else{
									$ticheng = '0';
								}
								
								//计算提成
								$person_price1 = Db::name('goods_shop_price')->where('shop_id', $find['shop_id'])->where('goods_id', $assignor_content)->count();
								if($person_price1=='0'){
									$person_price ='0'; 
									$price ='0'; 
								}else{
									$person_price = Db::name('goods_shop_price')->where('shop_id', $find['shop_id'])->where('goods_id', $assignor_content)->value('price');
								}
								$number_s = Db::name('order_goods')->where('order_id', $find['id'])->where('goods_id',$assignor_content)->value('number');  
								//计算总提成
								$assignor_price = ($ticheng + $person_price)*$number_s;
								$param['assignor_price'][$i] = $assignor_price;
							}
							/******************************/
							
					
					
						


                    $shop_assignor_records[$i]['order_id'] = $find['id'];

                    $shop_assignor_records[$i]['shop_id'] = $find['shop_id'];

                    $shop_assignor_records[$i]['assignor_id'] = $param['assignor_id'][$i];

                    $shop_assignor_records[$i]['single_id'] = $param['single_id'][$i];
					$shop_assignor_records[$i]['pici'] = $param['pici'][$i];
                    $shop_assignor_records[$i]['assignor_price'] = $param['assignor_price'][$i];

                    $shop_assignor_records[$i]['type'] = $param['typea'][$i];
					$shop_assignor_records[$i]['post_id'] = $param['post_id'][$i];

                    $shop_assignor_records[$i]['assignor_content'] = $assignor_content;

                    $shop_assignor_records[$i]['create_time'] = date('Y-m-d H:i:s', time());

                    $shop_assignor_records[$i]['order_type'] = $find['order_type'];

                    $shop_assignor_records[$i]['is_agree'] = 1;
                    $shop_assignor_records[$i]['is_assignor_agree'] = 1;
					
					//新增日志
					//根据类型判断是服务提成还是销售提成
					if($shop_assignor_records[$i]['type']=='1'){
						$content_a =$content_a.'提成类型:服务提成'; 
					}elseif($shop_assignor_records[$i]['type']=='2'){
						$content_a =$content_a.'提成类型：售卖提成'; 
					}
					///服务门店
					$shop_title = Db::name('shop')->where('id',$find['shop_id'])->value('shop_title');
					$order_num_a = Db::name('order')->where('id',$find['id'])->value('order_number');
					$content_a = $content_a."--门店名称:".$shop_title;
					//查询提交的这个信息
					$content_a = $content_a."--提成金额：".$param['assignor_price'][$i];
					$content_a=$content_a."--服务人员：".Db::name('shop_assignor')->where('id',$param['assignor_id'][$i])->value('name');
					$content_a=$content_a."--消费日期：".date('Y-m-d H:i:s',time());
					$content_a=$content_a."--服务内容：".Db::name('shop_goods')->where('id',$param['single_id'][$i])->value('title');
					$insert_log =  Db::name('member_log')->insert(
					 ['type_status' => 1,
					 'member_name' => \session('manage_user'),
					 'content' => $content_a,
					 'add_time' => time(),
					 'add_time_date'=> date('Y-m-d H:i:s',time()),
					 'type' => 1,
					 'member_id' => \session('manage_id'),
					 'member_jibie' => \session('manage_nick'),
					 'order_id' => $find['id'],
					 'order_num' => $order_num_a,
					 'order_type'=>$param['typea'][$i],
					]);
					
					
					//新增日志
					
					
					
					
					
					

//                        Db::name('gift_assignor_records')->insert(

//                            ['assignor_id' => $param['assignor_id'][$i],

//                                'assignor_price' => $param['assignor_price'][$i],

//                                'order_id' => $find['id'],

//                                'order_type' => $find['order_type'],

//                                'assignor_content' => $param['assignor_content'][$i],

//                                'create_time' => date('Y-m-d H:i:s',time()),

//                                'shop_id' => $shop_id

//                            ]);

                }





                if (isset($param['status']) && ($param['status'] == 3)) {

                    $ret = Db::name('order')->where('id', $param['id'])->update(['status' => 3]);

                }



                $r = Db::name('shop_assignor_records')->insertAll($shop_assignor_records);

                //halt(Db::getLastInsID());

                $last_id = Db::getLastInsID();

                $title_array = Db::name('order_goods')->where('order_id',$find['id'])->column('title');

                $goods_title = implode(',',$title_array);

                (new \app\index\controller\MemberMessage())->jpushMessage($find['member_id'], '商品订单确认', '商家已确认订单,请同意' , 4);

                Db::name('member_message')->insert(

                    ['member_id' => $find['member_id'],

                        'order_number' => $last_id,

                        'type_id' => 4,

                        'title' => $goods_title,

                        'content' => '商家已确认订单,请同意' ,

                        'is_click' => 1,

                        'create_time' => date('Y-m-d H:i:s', time())

                    ]);

                wingo('修改成功', Url::build('index'));



            }

            if (isset($param['status']) && ($param['status'] == 3)) {



                Db::name('order o')->where('id',$id)->where('status',2)->setField('status',3);

                wingo('修改成功', Url::build('index'));

            }

            if (isset($param['status']) && ($param['status'] >= 10)) {



                $fail_explain = Request::instance()->param('fail_explain','');

                if(strpos('5,7,8,9,10',(string)$level) === false){

                    fail('当前管理员无退款审核权限');

                }

                $refund_order_audit = Db::name('refund_order_audit')->where([

                    'order_id' => $find['id'],

                    'manage_id' => \session('manage_id'),

                ])->find();//当前管理员自己提交的数据

                if(!empty($refund_order_audit)) fail('已经申请过审核');

                if($level == 5){ //门店点击提交 店长-副经理-经理-财务-总经理

                    if(empty($refund_order_audit)){

                        (new RefundOrderAudit())->save([

                            'order_id' => $find['id'],

                            'manage_id' => \session('manage_id'),

                            'status' => input('post.status'),

                            'is_read' => 1,

                            'level' => $level,

                            'shop_id' => \session('shop_id'),

                            'shop_title' => Db::name('shop')->where('id',\session('shop_id'))->value('shop_title'),

                            'fail_explain' => $fail_explain,

                            'create_time' => date('Y-m-d H:i:s',time())

                        ]);

                        Db::name('order')->where('id',$find['id'])->setField('is_read_5',1);

                        if($param['status'] == 12){

                            Db::name('order')->where('id',$find['id'])->setField('status',12);

                        }

                        wingo('门店申请退款审核',Url::build('index'));

                    }else{

                        fail('已经申请审核');

                    }

                }

                $refund_order_audit_5 = Db::name('refund_order_audit')->where(['order_id' => $find['id'],'status' => 11, 'level' =>5])->find();//门店是否提交审核数据

                $refund_order_audit_7 = Db::name('refund_order_audit')->where(['order_id' => $find['id'],'status' => 11, 'level' =>7])->find();//门店是否提交审核数据

                $refund_order_audit_8 = Db::name('refund_order_audit')->where(['order_id' => $find['id'],'status' => 11, 'level' =>8])->find();//门店是否提交审核数据

                $refund_order_audit_9 = Db::name('refund_order_audit')->where(['order_id' => $find['id'],'status' => 11, 'level' =>9])->find();//门店是否提交审核数据

                if($level == 7){ //副经理

                    if(empty($refund_order_audit_5)) fail('门店没有通过退款审核');

                    $ret = (new RefundOrderAudit())->save([

                        'order_id' => $find['id'],

                        'manage_id' => \session('manage_id'),

                        'status' => input('post.status'),

                        'is_read' => 1,

                        'level' => $level,

                        'shop_id' => 0,

                        'fail_explain' => $fail_explain,

                        'create_time' => date('Y-m-d H:i:s',time())

                    ]);

                    Db::name('order')->where('id',$find['id'])->setField('is_read_7',1);

                    if($param['status'] == 12){

                        Db::name('order')->where('id',$find['id'])->setField('status',12);

                    }

                    if($ret){

                        wingo('副经理申请退款审核',Url::build('index'));

                    }



                }

                if($level == 8){ //经理



                    if(empty($refund_order_audit_5)) fail('门店没有通过退款审核');

                    if(empty($refund_order_audit_7)) fail('副经理没有通过退款审核');

                    $ret = (new RefundOrderAudit())->save([

                        'order_id' => $find['id'],

                        'manage_id' => \session('manage_id'),

                        'status' => input('post.status'),

                        'is_read' => 1,

                        'level' => $level,

                        'shop_id' => 0,

                        'fail_explain' => $fail_explain,

                        'create_time' => date('Y-m-d H:i:s',time())

                    ]);

                    Db::name('order')->where('id',$find['id'])->setField('is_read_8',1);

                    if($param['status'] == 12){

                        Db::name('order')->where('id',$find['id'])->setField('status',12);

                    }

                    if($ret){

                        wingo('经理申请退款审核',Url::build('index'));

                    }

                }

                if($level == 9){ //财务



                    if(empty($refund_order_audit_5)) fail('门店没有通过退款审核');

                    if(empty($refund_order_audit_7)) fail('副经理没有通过退款审核');

                    if(empty($refund_order_audit_8)) fail('经理没有通过退款审核');

                    $ret = (new RefundOrderAudit())->save([

                        'order_id' => $find['id'],

                        'manage_id' => \session('manage_id'),

                        'status' => input('post.status'),

                        'is_read' => 1,

                        'level' => $level,

                        'shop_id' => 0,

                        'fail_explain' => $fail_explain,

                        'create_time' => date('Y-m-d H:i:s',time())

                    ]);

                    Db::name('order')->where('id',$find['id'])->setField('is_read_9',1);

                    if($param['status'] == 12){

                        Db::name('order')->where('id',$find['id'])->setField('status',12);

                    }

                    if($ret){

                        wingo('财务申请退款审核',Url::build('index'));

                    }

                }

                if($level == 10){ //总经理



                    if(empty($refund_order_audit_5)) fail('门店没有通过退款审核');

                    if(empty($refund_order_audit_7)) fail('副经理没有通过退款审核');

                    if(empty($refund_order_audit_8)) fail('经理没有通过退款审核');

                    if(empty($refund_order_audit_9)) fail('财务没有通过退款审核');

                    Db::name('order')->where('id',$find['id'])->setField('is_read_10',1);

                    if($param['status'] == 12){

                        Db::name('order')->where('id',$find['id'])->setField('status',12);

                    }

                    if($param['status'] == 11){

                        $find = Db::name('order')->find($id);

                        if ($find['pay_type'] == '2') {

                            $ret = (new Alipay())->refund($find['order_number'], $refund['amount'], '', $find['shop_order_number']);

                            if ($ret->code !== '10000') {

                                fail($ret->sub_msg);

                            }

                            //halt($data['order_number'] . '-' . $data['price'] . '-' . $data['explain']);

                        } else if ($find['pay_type'] == '1') {

                            //halt('wx');

                            $ret = (new Alipay())->wechat($find['order_number'], $find['amount'], $refund['amount']);

                            if ($ret['result_code'] == 'FAIL') fail($ret['err_code_des']);

                        }

                        $ret = Db::name('order')->where('id', $param['id'])->update(['status' => input('post.status')]);

                        if ($ret !== false) {

                            (new Order())->refund($find['member_id'], $find['order_number']);

                            $refund_ret = Db::name('order_refund')->where('order_id', $id)->update(['fail_explain' => $param['fail_explain']]);

                            if ($refund_ret !== false) {

                                wingo('退货款处理完毕', Url::build('index'));

                            }

                        }

                        $ret = (new RefundOrderAudit())->save([

                            'order_id' => $find['id'],

                            'manage_id' => \session('manage_id'),

                            'status' => input('post.status'),

                            'is_read' => 1,

                            'level' => $level,

                            'shop_id' => 0,

                            'fail_explain' => $fail_explain,

                            'create_time' => date('Y-m-d H:i:s',time())

                        ]);

                        $order_goodstuikuan = Db::name('order_goods')->where('order_id', $find['id'])->select();

                        if (!empty($order_goodstuikuan)) {

                            foreach ($order_goodstuikuan as $k => $v) {

                                Db::name('shop_spec_attr')->where(['attribute' => $v['attribute'], 'goods_id' => $v['goods_id']])->where('shop_id', $v['shop_id'])->setInc('inventory', $v['number']);

                                $sale_number = Db::name('shop_goods')->where('id', $v['goods_id'])->value('sale_number');

                                if ($sale_number >= $v['number']) {

                                    Db::name('shop_goods')->where('id', $v['goods_id'])->setDec('sale_number', $v['number']);

                                }

                            }

                        }

                        if($ret){

                            Db::name('member_distribution_records')->where(['order_number' => $find['order_number']])->delete();

                            wingo('总经理同意退款中',Url::build('index'));

                        }

                    }



                }











            } //退款逻辑end

//            if (isset($param['status']) && ($param['status'] == 11)) {

//                $find = Db::name('order')->find($id);

//                if ($find['pay_type'] == '2') {

//                    $ret = (new Alipay())->refund($find['order_number'], $refund['amount'], '',$find['shop_order_number']);

//                    if($ret->code !== '10000'){

//                        fail($ret->sub_msg);

//                    }

//                    //halt($data['order_number'] . '-' . $data['price'] . '-' . $data['explain']);

//                } else if ($find['pay_type'] == '1') {

//                    //halt('wx');

//                    $ret = (new Alipay())->wechat($find['order_number'], $find['amount'], $refund['amount']);

//                    if($ret['result_code'] == 'FAIL') fail($ret['err_code_des']);

//                }

//                $ret = Db::name('order')->where('id', $param['id'])->update(['status' => $param['status']]);

//                if ($ret !== false) {

//                    $this->refund($find['member_id'],$find['order_number']);

//                    $refund_ret = Db::name('order_refund')->where('order_id',$id)->update(['fail_explain' =>$param['fail_explain']]);

//                    if($refund_ret !== false){

//                        win('退货款处理完毕', Url::build('index'));

//                    }

//                }

//            }

//

//            if (isset($param['status']) && ($param['status'] == 12)) {

//                $ret = Db::name('order')->where('id', $param['id'])->update(['status' => $param['status']]);

//                if ($ret !== false) {

//                    $refund_ret = Db::name('order_refund')->where('order_id',$id)->update(['fail_explain' =>$param['fail_explain']]);

//                    if($refund_ret !== false){

//                        win('退货款失败处理完毕', Url::build('index'));

//                    }

//                }

//            }

        }

        $info = Db::name('order o')

            ->join('member m', 'o.member_id = m.id','left')

            ->join('shop s', 'o.shop_id = s.id','left')

            ->field('o.*,m.username,s.shop_title,m.car_number,m.brand_number,m.id_code')

            ->where('o.id', $param['id'])

            ->find();

        if($info['status'] == 10 || $info['status'] == 11 || $info['status'] == 12){

            $info['refund'] = Db::name('order_refund')->where('order_id',$info['id'])->field('explain,amount price,type')->find();

            $info['refund_create_time'] = Db::name('order_refund')->where('order_id',$info['id'])->value('create_time');

        }

        $info['goods_list'] = Db::name('order_goods og')

            ->join('shop_goods sg', 'og.goods_id = sg.id','LEFT')

            ->field('og.*,sg.thumb_img')

            ->where('order_id', $id)

            ->select();



        $refund_order_audit_records = (new RefundOrderAudit())

            ->alias('roa')

            ->join('manager m','roa.manage_id = m.id')

            ->field('roa.*,m.username')

            ->where('order_id',$find['id'])

            ->select();

        //审核会员数量 不管通过不通过;

        $count = (new RefundOrderAudit())->where('order_id',$find['id'])->where('status','11')->count();

        $refund_button = false; //这么判断 其他人员肯定是false

        if($level == 5){

            if($count == 0){

                $refund_button = true;

            }

        }

        if($level == 7){

            if($count == 1){

                $refund_button = true;

            }

        }

        if($level == 8){

            if($count == 2){

                $refund_button = true;

            }

        }

        if($level == 9){

            if($count == 3){

                $refund_button = true;

            }

        }

        if($level == 10){

            if($count == 4){

                $refund_button = true;

            }

        }

        $is_agreee = '1,2';

        if(isset($param['table'])) $is_agreee = 2;

        if(\session('role_id') == 1){

            $is_show = [];

        }else{

            $is_show['sar.is_show'] = ['eq',1];

        }

        $shop_assignor_records = Db::name('shop_assignor_records sar')

            ->join('shop','shop.id = sar.shop_id','left')
			->join('job jb','jb.id = sar.post_id','left')
            ->join('shop_assignor sa','sar.assignor_id = sa.id','left')

            ->join('shop_goods gt','gt.id = sar.single_id','left')

            ->where('order_id', $info['id'])

            ->where($is_show)

            ->where('sar.is_agree','in',$is_agreee)

            ->field('shop.shop_title,gt.title,sa.*,sar.*,jb.title as post_title')

            ->order('sar.id','desc')

            ->select()->toArray();

		$shop_assignor_records1 = Db::name('shop_assignor_records sar')

            ->join('shop','shop.id = sar.shop_id','left')
			->join('job jb','jb.id = sar.post_id','left')
            ->join('shop_assignor sa','sar.assignor_id = sa.id','left')

            ->join('shop_goods gt','gt.id = sar.single_id','left')

            ->where('order_id', $info['id'])
			->where('assignor_id','neq','')

            //->where($is_show)

            //->where('sar.is_agree','in',$is_agreee)

            ->field('shop.shop_title,gt.title,sa.*,sar.*,jb.title as post_title')

            ->order('sar.id','desc')

            ->select()->toArray();
			
			$shop_assignor_records2 = Db::name('shop_assignor_records sar')

            ->join('shop','shop.id = sar.shop_id','left')
			->join('job jb','jb.id = sar.post_id','left')
            ->join('shop_assignor sa','sar.assignor_id = sa.id','left')

            ->join('shop_goods gt','gt.id = sar.single_id','left')

            ->where('order_id', $info['id'])
			//->where('assignor_id','neq','')

            //->where($is_show)

            //->where('sar.is_agree','in',$is_agreee)

            ->field('shop.shop_title,gt.title,sa.*,sar.*,jb.title as post_title')

            ->order('sar.id','desc')

            ->select()->toArray();

        if (!empty($shop_assignor_records)) {

            foreach ($shop_assignor_records as $k => &$v) {

                if($v['type'] == 1){

                    $v['type'] = '服务提成';

                }

                if($v['type'] == 2){

                    $v['type'] = '销售提成';

                }

                if ($v['order_type'] == 2) {

                    $v['single_name'] = $v['cat_name'];

                }

                if ($v['order_type'] == 1) {

                    $v['single_name'] = $v['title'];

                }

                if($v['type'] == "服务提成"){



                    //$v['title'] = Db::name('single')->where('id',$v['assignor_content'])->value('cat_name').'=>'.Db::name('single')->where('id',$v['assignor_id'])->value('cat_name');

                }else{



                }

                //$v['assignor_content'] = Db::name('single')->where('id',$v['assignor_content'])->value('cat_name');

               // $v['assignor_content'] = $v['title'];



            }

        }
		if (!empty($shop_assignor_records1)) {

            foreach ($shop_assignor_records1 as $k => &$v) {

                if($v['type'] == 1){

                    $v['type'] = '服务提成';

                }

                if($v['type'] == 2){

                    $v['type'] = '销售提成';

                }

                if ($v['order_type'] == 2) {

                    $v['single_name'] = $v['cat_name'];

                }

                if ($v['order_type'] == 1) {

                    $v['single_name'] = $v['title'];

                }

                if($v['type'] == "服务提成"){



                    //$v['title'] = Db::name('single')->where('id',$v['assignor_content'])->value('cat_name').'=>'.Db::name('single')->where('id',$v['assignor_id'])->value('cat_name');

                }else{



                }

                //$v['assignor_content'] = Db::name('single')->where('id',$v['assignor_content'])->value('cat_name');

               // $v['assignor_content'] = $v['title'];



            }

        }
		
		if (!empty($shop_assignor_records2)) {

            foreach ($shop_assignor_records2 as $k => &$v) {

                if($v['type'] == 1){

                    $v['type'] = '服务提成';

                }

                if($v['type'] == 2){

                    $v['type'] = '销售提成';

                }

                if ($v['order_type'] == 2) {

                    $v['single_name'] = $v['cat_name'];

                }

                if ($v['order_type'] == 1) {

                    $v['single_name'] = $v['title'];

                }

                if($v['type'] == "服务提成"){



                    //$v['title'] = Db::name('single')->where('id',$v['assignor_content'])->value('cat_name').'=>'.Db::name('single')->where('id',$v['assignor_id'])->value('cat_name');

                }else{



                }

                //$v['assignor_content'] = Db::name('single')->where('id',$v['assignor_content'])->value('cat_name');

               // $v['assignor_content'] = $v['title'];



            }

        }
		
		



        $shop_assignor = Db::name('shop_assignor')->where('is_delete',1)->select();

        $service_people_price = Db::name('shop_service_price')

            ->alias('ssp')

            ->join('single s','ssp.single_id = s.id','left')

            ->where('shop_id',$find['shop_id'])

            ->where('s.parent_id','eq',0)

            ->where('auding_status',2)

            ->where('is_delete',1)

            ->field('s.id,s.cat_name,ssp.person_price')

            ->select();



        if(session('role_id') != 1){

            $info['username'] = hideStr($info['username'],1,6);

        }

		$post_list = Db::name('job jb')->join('shop s','jb.shop_id = s.id','left')->field('jb.*,s.shop_title')->where('jb.status',1)->select();

        $service_people_price2 = Db::name('single')

            ->where('auding_status',2)

            ->where('is_delete',1)

            ->where('parent_id',1)

            ->select();

        $order_goods = Db::name('order_goods')

            ->alias('og')

            ->where('og.order_id',$info['id'])

            ->join('shop_goods sg','og.goods_id = sg.id','left')

            ->join('goods_type gt','gt.id = sg.goods_id','left')

            ->group('gt.id')

            ->field('og.goods_id,og.shop_id,og.order_id,gt.id,sum(number) number,og.title')

            ->select()->toArray();

        $go = '';



        foreach ($order_goods as $k1 => $v1){

            $go .= $v1['title'].' ';

        }

        // 必须$v1 否则重复定义 halt($shop_assignor_records);

        $renyuan['one_str'] = '';

        $renyuan['three_str'] = '';

        $renyuan['last_person_price'] = '';

        if(!empty($order_goods)){

            foreach ($order_goods as $k => &$v){

                $v['three_level_id'] = $v['id'];

                $v['three_level_name'] = Db::name('goods_type')->where('id',$v['id'])->value('title');

                $price = Db::name('goods_type')->where('id',$v['id'])->value('price');

                $two_id = Db::name('goods_type')->where('id',$v['id'])->value('parenTid');

                $v['two_level_id'] = Db::name('goods_type')->where('id',$v['id'])->value('parenTid');

                $v['two_level_name'] = Db::name('goods_type')->where('id',$two_id)->value('title');

                $v['one_level_id'] = Db::name('goods_type')->where('id',$two_id)->value('parenTid');

                $three_id = Db::name('goods_type')->where('id',$two_id)->value('parenTid');

                $v['one_level_name'] = Db::name('goods_type')->where('id',$three_id)->value('title');

                $v['person_price'] = $price*$v['number'];

                $renyuan['one_str'] .= $v['one_level_name'].' ';

                $renyuan['three_str'] .= $v['three_level_name'].' ';

                $renyuan['last_person_price'] += $price*$v['number'];

            }

        }

        $template = '';

        if(isset($param['table'])) $template = 'service_order_table';

        $info['cat_name'] = $go;

        $info['one_cat'] = '';

        $info['order_number'] = $info['shop_order_number'];

        $order_goods = $info['goods_list']->toArray();

        $goods_price = Db::name('goods_shop_price')->where('goods_id',$order_goods[0]['goods_id'])->where('shop_id',$order_goods[0]['shop_id'])->value('price');

        $goods_price =  $goods_price * $v['number'];
        //var_dump($v['number']);

        if(is_null($goods_price)) $goods_price = 0;

        $shop_bolen = true;







        $shop_list = Db::name('shop')->order('shop_status','asc')->select()->toArray();



        if(!empty($order_goods)){ //符合条件的门店商品list

            foreach ($order_goods as $k => $v){

                foreach ($shop_list as $k1 => $v1){

                    $shop_where['shop_id'] = $v1['id'];

                    $shop_where['goods_id'] = $v['goods_id'];

                    $shop_where['attribute'] = $v['attribute'];

                    $shop_find = Db::name('shop_spec_attr')->where($shop_where)->find();

                    //dump($shop_find['shop_id']);

                    if(empty($shop_find) || $shop_find['inventory'] < $v['number']){

                      unset($shop_list[$k]);

                    }

                }

            }

        }



        $service_people_price = Db::name('single')->field('id,cat_name,parent_id,sort')->where('is_delete',1)->where('auding_status',2)->where('parent_id',0)->order('sort asc')->select()->toArray(); //服务人员提成;



        $service_people_price2 = Db::name('single')->field('id,cat_name,parent_id,sort')->where('is_delete',1)->where('auding_status',2)->where('parent_id',$service_people_price[0]['id'])->order('sort asc')->select()->toArray(); //服务人员提成;;

        foreach ($service_people_price2 as &$v){

            $price = Db::name('single_shop_price')->where('shop_id',$find['shop_id'])->where('single_id',$v['id'])->value('price');

            if(is_null($price)) $price = 0.00;

            $v['person_price'] = $price;

        }





        $mwhere['status'] = ['eq',1];

       // $mwhere['start_time'] = ['elt',time()];

        //$mwhere['end_time'] = ['gt',time()];

        $mwhere['type'] = ['in','1,3'];



        $result = (new  \app\index\model\MemberCoupon)->where($mwhere)

            ->field('id,type,single_id,title,minus_price,total_price,content,status,start_time,end_time')

            ->where('total_price','elt',$info['order_price'])

            ->order(['id' => 'desc'])

            ->fetchSql(false)

            //->whereOr('type',3)

            ->where('member_id',$info['member_id'])

            ->select();



        $member_car_list1 = Db::name('member')->where('id',$info['member_id'])->column('car_number');

        $member_car_list2 = Db::name('member_car_list')->where('member_id',$info['member_id'])->group('car_number')->column('car_number');

        $member_car_list3 = Db::name('order')->where('member_id',$info['member_id'])->group('car_number')->column('car_number');

        $member_car_list4 = array_merge($member_car_list1,$member_car_list2,$member_car_list3);

        $member_car_list4 = array_unique($member_car_list4);

        if(!empty($member_car_list4)){

            foreach ($member_car_list4 as $k =>&$v){

                if(empty($v)){

                    unset($member_car_list4[$k]);

                }

            }

        }

        $user_coupon = http_gets("http://lihai.xmdonkey.com/index.php/index/member_coupon/index/member_id/{$info['member_id']}/status/1/order_price/{$info['order_price']}/type/1/single_id/0}");

        $coupon_array = json_decode($user_coupon,true)['data']['data'];

//        dump($coupon_array);

		$shop_list1 = Db::name('shop')
		->where('delete_time','')
		->where('auding_status','2')
		->where('auding_status1','2')
		->where('auding_status2','2')
		->where('auding_status3','2')
		->order('id','asc')
		
		->select();
//////////////////////////////////////批次///////////////////////////////////////////////////////////
		$pici_status = Db::name('shop_assignor_records')->where('order_id',$info['id'])->count();
		if($pici_status =='0'){
			$pici = '1';
			$picia = '1';
		}else{
			//查询之后一个批次的数
			$pici = Db::name('shop_assignor_records')->where('order_id',$info['id'])->order('pici','desc')->value('pici') + 1;
			$picia =  Db::name('shop_assignor_records')->where('order_id',$info['id'])->order('pici','desc')->value('pici');
			
			
			
		}
		
		
		
		if(\session('role_id')=='5'){
			$pici_list = Db::name('shop_assignor_records')->where('order_id',$info['id'])->where('pici_status','1')->Distinct(true)->field('pici')->select()->toArray();
			foreach($pici_list as $k => &$v){
				$v['shifou_tongyi'] = Db::name('shop_assignor_records')->where('order_id',$info['id'])->where('pici',$v['pici'])->where('is_agree','1')->count();
				if($v['shifou_tongyi'] > '0'){
					$v['is_agree_agree'] = '0';
				}else{
					$v['is_agree_agree']='1';
				}
			}
		}else{
			$pici_list = Db::name('shop_assignor_records')->where('order_id',$info['id'])->Distinct(true)->field('pici')->select()->toArray();
			foreach($pici_list as $k => &$v){
				$v['is_agree_agree']='1';
			}
		}
		
		//var_dump($pici_list);
		//////////////////////////////////////批次///////////////////////////////////////////////////////////
        return view($template, ['item' => $info,

            'shop_assignor' => $shop_assignor,
			
            'member_car_list4' => $member_car_list4,

            'shop_list' => $shop_list1,

            'shop_id' => $order_goods[0]['shop_id'],

            'goods_price' => $goods_price,
			'pici_list' => $pici_list,

            'order_goods' => json_encode($order_goods),
			
			'post_list' => $post_list,
			'pici' => $pici,
			'picia' => $picia,

            'service_people_price' => json_encode($service_people_price),

            'member_coupon' => $result,
			'shop_assignor_records1' => $shop_assignor_records1,

            'service_people_price2' => json_encode($service_people_price2),

            'refund' => $refund,'refund_order_audit_records' => $refund_order_audit_records,

            'refund_button' => $refund_button,

            'category' => json_encode(ClassifyAlias::dimension(Cache::get('goodsType'))),

            'shop_assignor_records' => $shop_assignor_records,

            'gift_assignor_records' => $shop_assignor_records2,]);

    }
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


    public function update_dayin($id)

    {



        $param = Request::instance()->param();

        $level = \session('role_id');//当前登录的管理员界别 5 7 8 9 10

        $find = Db::name('order')->find($id);



        $auding_status = Session::get('auding_status');

        if($auding_status == 2){

            Db::name('order o')->where('id',$id)->setField('is_read',1);

        }else{

            Db::name('order o')->where('id',$id)->setField('is_read_shop',1);

        }



        $refund = Db::name('order_refund ors')->field('ors.*,rr.title')->where('order_id',$find['id'])->join('refund_reason rr','ors.reason_id = rr.id')->find();

        if (Request::instance()->isPost()) {



            if(!empty($param['assignor_id'])){

                if (count($param['assignor_id']) != count(array_unique($param['assignor_id']))) {

                    //fail('不能添加重复人员');

                }

                //$single_id = explode('-',$param['single_id'][0])[0];//门店服务分类 第一个类就代表所有相同的服务类了

                // $single_title = Db::name('single')->where('id',$single_id)->value('cat_name');//服务类型

                // $assignor_price = explode('-',$param['single_id'][0])[1];//门店金额

                for ($i = 0; $i < count($param['assignor_id']); $i++) {

                    $shop_assignor_records[$i]['order_id'] = $find['id'];

                    $shop_assignor_records[$i]['shop_id'] = $find['shop_id'];

                    $shop_assignor_records[$i]['assignor_id'] = $param['assignor_id'][$i];

                    $shop_assignor_records[$i]['single_id'] = $param['single_id'][$i];

                    $shop_assignor_records[$i]['assignor_price'] = $param['assignor_price'][$i];

                    $shop_assignor_records[$i]['assignor_content'] = $param['single_id'][$i];

                    $shop_assignor_records[$i]['create_time'] = date('Y-m-d H:i:s', time());

                    $shop_assignor_records[$i]['order_type'] = $find['order_type'];

                    $shop_assignor_records[$i]['is_agree'] = 2;

//                        Db::name('gift_assignor_records')->insert(

//                            ['assignor_id' => $param['assignor_id'][$i],

//                                'assignor_price' => $param['assignor_price'][$i],

//                                'order_id' => $find['id'],

//                                'order_type' => $find['order_type'],

//                                'assignor_content' => $param['assignor_content'][$i],

//                                'create_time' => date('Y-m-d H:i:s',time()),

//                                'shop_id' => $shop_id

//                            ]);

                }

                if (isset($param['status']) && ($param['status'] == 3)) {

                    $ret = Db::name('order')->where('id', $param['id'])->update(['status' => 3]);

                }



                Db::name('shop_assignor_records')->insertAll($shop_assignor_records);

                wingo('修改成功', Url::build('index'));



            }

            if (isset($param['status']) && ($param['status'] == 3)) {



                Db::name('order o')->where('id',$id)->where('status',2)->setField('status',3);

                wingo('修改成功', Url::build('index'));

            }

            if (isset($param['status']) && ($param['status'] >= 10)) {



                $fail_explain = Request::instance()->param('fail_explain','');

                if(strpos('5,7,8,9,10',(string)$level) === false){

                    fail('当前管理员无退款审核权限');

                }

                $refund_order_audit = Db::name('refund_order_audit')->where([

                    'order_id' => $find['id'],

                    'manage_id' => \session('manage_id'),

                ])->find();//当前管理员自己提交的数据

                if(!empty($refund_order_audit)) fail('已经申请过审核');

                if($level == 5){ //门店点击提交 店长-副经理-经理-财务-总经理

                    if(empty($refund_order_audit)){

                        (new RefundOrderAudit())->save([

                            'order_id' => $find['id'],

                            'manage_id' => \session('manage_id'),

                            'status' => input('post.status'),

                            'is_read' => 1,

                            'level' => $level,

                            'shop_id' => \session('shop_id'),

                            'shop_title' => Db::name('shop')->where('id',\session('shop_id'))->value('shop_title'),

                            'fail_explain' => $fail_explain,

                            'create_time' => date('Y-m-d H:i:s',time())

                        ]);

                        Db::name('order')->where('id',$find['id'])->setField('is_read_5',1);

                        if($param['status'] == 12){

                            Db::name('order')->where('id',$find['id'])->setField('status',12);

                        }

                        wingo('门店申请退款审核',Url::build('index'));

                    }else{

                        fail('已经申请审核');

                    }

                }

                $refund_order_audit_5 = Db::name('refund_order_audit')->where(['order_id' => $find['id'],'status' => 11, 'level' =>5])->find();//门店是否提交审核数据

                $refund_order_audit_7 = Db::name('refund_order_audit')->where(['order_id' => $find['id'],'status' => 11, 'level' =>7])->find();//门店是否提交审核数据

                $refund_order_audit_8 = Db::name('refund_order_audit')->where(['order_id' => $find['id'],'status' => 11, 'level' =>8])->find();//门店是否提交审核数据

                $refund_order_audit_9 = Db::name('refund_order_audit')->where(['order_id' => $find['id'],'status' => 11, 'level' =>9])->find();//门店是否提交审核数据

                if($level == 7){ //副经理

                    if(empty($refund_order_audit_5)) fail('门店没有通过退款审核');

                    $ret = (new RefundOrderAudit())->save([

                        'order_id' => $find['id'],

                        'manage_id' => \session('manage_id'),

                        'status' => input('post.status'),

                        'is_read' => 1,

                        'level' => $level,

                        'shop_id' => 0,

                        'fail_explain' => $fail_explain,

                        'create_time' => date('Y-m-d H:i:s',time())

                    ]);

                    Db::name('order')->where('id',$find['id'])->setField('is_read_7',1);

                    if($param['status'] == 12){

                        Db::name('order')->where('id',$find['id'])->setField('status',12);

                    }

                    if($ret){

                        wingo('副经理申请退款审核',Url::build('index'));

                    }



                }

                if($level == 8){ //经理



                    if(empty($refund_order_audit_5)) fail('门店没有通过退款审核');

                    if(empty($refund_order_audit_7)) fail('副经理没有通过退款审核');

                    $ret = (new RefundOrderAudit())->save([

                        'order_id' => $find['id'],

                        'manage_id' => \session('manage_id'),

                        'status' => input('post.status'),

                        'is_read' => 1,

                        'level' => $level,

                        'shop_id' => 0,

                        'fail_explain' => $fail_explain,

                        'create_time' => date('Y-m-d H:i:s',time())

                    ]);

                    Db::name('order')->where('id',$find['id'])->setField('is_read_8',1);

                    if($param['status'] == 12){

                        Db::name('order')->where('id',$find['id'])->setField('status',12);

                    }

                    if($ret){

                        wingo('经理申请退款审核',Url::build('index'));

                    }

                }

                if($level == 9){ //财务



                    if(empty($refund_order_audit_5)) fail('门店没有通过退款审核');

                    if(empty($refund_order_audit_7)) fail('副经理没有通过退款审核');

                    if(empty($refund_order_audit_8)) fail('经理没有通过退款审核');

                    $ret = (new RefundOrderAudit())->save([

                        'order_id' => $find['id'],

                        'manage_id' => \session('manage_id'),

                        'status' => input('post.status'),

                        'is_read' => 1,

                        'level' => $level,

                        'shop_id' => 0,

                        'fail_explain' => $fail_explain,

                        'create_time' => date('Y-m-d H:i:s',time())

                    ]);

                    Db::name('order')->where('id',$find['id'])->setField('is_read_9',1);

                    if($param['status'] == 12){

                        Db::name('order')->where('id',$find['id'])->setField('status',12);

                    }

                    if($ret){

                        wingo('财务申请退款审核',Url::build('index'));

                    }

                }

                if($level == 10){ //总经理



                    if(empty($refund_order_audit_5)) fail('门店没有通过退款审核');

                    if(empty($refund_order_audit_7)) fail('副经理没有通过退款审核');

                    if(empty($refund_order_audit_8)) fail('经理没有通过退款审核');

                    if(empty($refund_order_audit_9)) fail('财务没有通过退款审核');

                    Db::name('order')->where('id',$find['id'])->setField('is_read_10',1);

                    if($param['status'] == 12){

                        Db::name('order')->where('id',$find['id'])->setField('status',12);

                    }

                    if($param['status'] == 11){

                        $find = Db::name('order')->find($id);

                        if ($find['pay_type'] == '2') {

                            $ret = (new Alipay())->refund($find['order_number'], $refund['amount'], '', $find['shop_order_number']);

                            if ($ret->code !== '10000') {

                                fail($ret->sub_msg);

                            }

                            //halt($data['order_number'] . '-' . $data['price'] . '-' . $data['explain']);

                        } else if ($find['pay_type'] == '1') {

                            //halt('wx');

                            $ret = (new Alipay())->wechat($find['order_number'], $find['amount'], $refund['amount']);

                            if ($ret['result_code'] == 'FAIL') fail($ret['err_code_des']);

                        }

                        $ret = Db::name('order')->where('id', $param['id'])->update(['status' => input('post.status')]);

                        if ($ret !== false) {

                            (new Order())->refund($find['member_id'], $find['order_number']);

                            $refund_ret = Db::name('order_refund')->where('order_id', $id)->update(['fail_explain' => $param['fail_explain']]);

                            if ($refund_ret !== false) {

                                wingo('退货款处理完毕', Url::build('index'));

                            }

                        }

                        $ret = (new RefundOrderAudit())->save([

                            'order_id' => $find['id'],

                            'manage_id' => \session('manage_id'),

                            'status' => input('post.status'),

                            'is_read' => 1,

                            'level' => $level,

                            'shop_id' => 0,

                            'fail_explain' => $fail_explain,

                            'create_time' => date('Y-m-d H:i:s',time())

                        ]);

                        if($ret){

                            wingo('总经理同意退款中',Url::build('index'));

                        }

                    }



                }











            } //退款逻辑end

//            if (isset($param['status']) && ($param['status'] == 11)) {

//                $find = Db::name('order')->find($id);

//                if ($find['pay_type'] == '2') {

//                    $ret = (new Alipay())->refund($find['order_number'], $refund['amount'], '',$find['shop_order_number']);

//                    if($ret->code !== '10000'){

//                        fail($ret->sub_msg);

//                    }

//                    //halt($data['order_number'] . '-' . $data['price'] . '-' . $data['explain']);

//                } else if ($find['pay_type'] == '1') {

//                    //halt('wx');

//                    $ret = (new Alipay())->wechat($find['order_number'], $find['amount'], $refund['amount']);

//                    if($ret['result_code'] == 'FAIL') fail($ret['err_code_des']);

//                }

//                $ret = Db::name('order')->where('id', $param['id'])->update(['status' => $param['status']]);

//                if ($ret !== false) {

//                    $this->refund($find['member_id'],$find['order_number']);

//                    $refund_ret = Db::name('order_refund')->where('order_id',$id)->update(['fail_explain' =>$param['fail_explain']]);

//                    if($refund_ret !== false){

//                        win('退货款处理完毕', Url::build('index'));

//                    }

//                }

//            }

//

//            if (isset($param['status']) && ($param['status'] == 12)) {

//                $ret = Db::name('order')->where('id', $param['id'])->update(['status' => $param['status']]);

//                if ($ret !== false) {

//                    $refund_ret = Db::name('order_refund')->where('order_id',$id)->update(['fail_explain' =>$param['fail_explain']]);

//                    if($refund_ret !== false){

//                        win('退货款失败处理完毕', Url::build('index'));

//                    }

//                }

//            }

        }

        $info = Db::name('order o')

            ->join('member m', 'o.member_id = m.id','left')

            ->join('shop s', 'o.shop_id = s.id','left')

            ->field('o.*,m.username,s.shop_title,m.car_number,m.brand_number,m.id_code')

            ->where('o.id', $param['id'])

            ->find();

        if($info['status'] == 10 || $info['status'] == 11 || $info['status'] == 12){

            $info['refund'] = Db::name('order_refund')->where('order_id',$info['id'])->field('explain,amount price,type')->find();

            $info['refund_create_time'] = Db::name('order_refund')->where('order_id',$info['id'])->value('create_time');

        }

        $info['goods_list'] = Db::name('order_goods og')

            ->join('shop_goods sg', 'og.goods_id = sg.id')

            ->field('og.*,sg.thumb_img')

            ->where('order_id', $id)

            ->select();



        $refund_order_audit_records = (new RefundOrderAudit())

            ->alias('roa')

            ->join('manager m','roa.manage_id = m.id')

            ->field('roa.*,m.username')

            ->where('order_id',$find['id'])

            ->select();

        //审核会员数量 不管通过不通过;

        $count = (new RefundOrderAudit())->where('order_id',$find['id'])->where('status','11')->count();

        $refund_button = false; //这么判断 其他人员肯定是false

        if($level == 5){

            if($count == 0){

                $refund_button = true;

            }

        }

        if($level == 7){

            if($count == 1){

                $refund_button = true;

            }

        }

        if($level == 8){

            if($count == 2){

                $refund_button = true;

            }

        }

        if($level == 9){

            if($count == 3){

                $refund_button = true;

            }

        }

        if($level == 10){

            if($count == 4){

                $refund_button = true;

            }

        }

        $shop_assignor_records = Db::name('shop_assignor_records sar')

            ->join('shop','shop.id = sar.shop_id','left')

            ->join('shop_assignor sa','sar.assignor_id = sa.id','left')

            ->join('shop_goods gt','gt.id = sar.single_id','left')

            ->where('order_id', $info['id'])

            ->field('shop.shop_title,gt.title,sa.*,sar.*')

            ->order('sar.id','desc')

            ->select()->toArray();



        if (!empty($shop_assignor_records)) {

            foreach ($shop_assignor_records as $k => &$v) {

                if ($v['order_type'] == 2) {

                    $v['single_name'] = $v['cat_name'];

                }

                if ($v['order_type'] == 1) {

                    $v['single_name'] = $v['title'];

                }

                //$v['assignor_content'] = Db::name('single')->where('id',$v['assignor_content'])->value('cat_name');

                $v['assignor_content'] = $v['title'];

            }

        }

        $shop_assignor = Db::name('shop_assignor')->where('shop_id',$find['shop_id'])->where('is_delete',1)->select();

        $service_people_price = Db::name('shop_service_price')

            ->alias('ssp')

            ->join('single s','ssp.single_id = s.id','left')

            ->where('shop_id',$find['shop_id'])

            ->where('s.parent_id','eq',0)

            ->where('auding_status',2)

            ->where('is_delete',1)

            ->field('s.id,s.cat_name,ssp.person_price')

            ->select();



        if(session('role_id') != 1){

            $info['username'] = hideStr($info['username'],1,6);

        }



        $service_people_price2 = Db::name('single')

            ->where('auding_status',2)

            ->where('is_delete',1)

            ->where('parent_id',1)

            ->select();

        $order_goods = Db::name('order_goods')

            ->alias('og')

            ->where('og.order_id',$info['id'])

            ->join('shop_goods sg','og.goods_id = sg.id')

            ->join('goods_type gt','gt.id = sg.goods_id')

            ->group('gt.id')

            ->field('og.goods_id,og.shop_id,og.order_id,gt.id,sum(number) number,og.title')

            ->select()->toArray();

        $go = '';



        foreach ($order_goods as $k1 => $v1){

            $go .= $v1['title'].' ';

        }

        // 必须$v1 否则重复定义 halt($shop_assignor_records);

        $renyuan['one_str'] = '';

        $renyuan['three_str'] = '';

        $renyuan['last_person_price'] = '';

        if(!empty($order_goods)){

            foreach ($order_goods as $k => &$v){

                $v['three_level_id'] = $v['id'];

                $v['three_level_name'] = Db::name('goods_type')->where('id',$v['id'])->value('title');

                $price = Db::name('goods_type')->where('id',$v['id'])->value('price');

                $two_id = Db::name('goods_type')->where('id',$v['id'])->value('parenTid');

                $v['two_level_id'] = Db::name('goods_type')->where('id',$v['id'])->value('parenTid');

                $v['two_level_name'] = Db::name('goods_type')->where('id',$two_id)->value('title');

                $v['one_level_id'] = Db::name('goods_type')->where('id',$two_id)->value('parenTid');

                $three_id = Db::name('goods_type')->where('id',$two_id)->value('parenTid');

                $v['one_level_name'] = Db::name('goods_type')->where('id',$three_id)->value('title');

                $v['person_price'] = $price*$v['number'];

                $renyuan['one_str'] .= $v['one_level_name'].' ';

                $renyuan['three_str'] .= $v['three_level_name'].' ';

                $renyuan['last_person_price'] += $price*$v['number'];

            }

        }

        $template = '';

        if(isset($param['table'])) $template = 'service_order_table';

        $info['cat_name'] = $go;

        $info['one_cat'] = '';

        $info['order_number'] = $info['shop_order_number'];



        $goods_price = Db::name('goods_shop_price')->where('goods_id',$order_goods[0]['goods_id'])->where('shop_id',$order_goods[0]['shop_id'])->value('price');



        if(is_null($goods_price)) $goods_price = 0;

        return view($template, ['item' => $info,

            'shop_id' => $order_goods[0]['shop_id'],

            'goods_price' => $goods_price,

            'order_goods' => json_encode($order_goods),

            'service_people_price' => $service_people_price,

            'shop_assignor' => $shop_assignor,

            'service_people_price2' => json_encode($service_people_price2),

            'refund' => $refund,'refund_order_audit_records' => $refund_order_audit_records,

            'refund_button' => $refund_button,

            'category' => json_encode(ClassifyAlias::dimension(Cache::get('goodsType'))),

            'shop_assignor_records' => $shop_assignor_records,

            'gift_assignor_records' => $shop_assignor_records2,]);

    }



    public function ajax_goods_shop_price($goods_id, $shop_id)

    {

        $goods_price = Db::name('goods_shop_price')->where('goods_id', $goods_id)->where('shop_id', $shop_id)->value('price');

        if(is_null($goods_price)) $goods_price = 0;

        return $goods_price;

    }

    public function ajax_goods_shop_price1($goods_id, $shop_id)

    {

        $goods_price = Db::name('goods_shop_price1')->where('goods_id', $goods_id)->where('shop_id', $shop_id)->value('price');

        if(is_null($goods_price)) $goods_price = 0;

        return $goods_price;

    }



    public function ajaxfenlei($id, $order_id)

    {

        $order_goods = Db::name('order_goods')

            ->alias('og')

            ->where('og.order_id', $order_id)

            ->where('gt.id',$id)

            ->join('shop_goods sg', 'og.goods_id = sg.id')

            ->join('goods_type gt', 'gt.id = sg.goods_id')

            ->group('gt.id')

            ->field('gt.id,sum(number) number')

            ->select()->toArray();



        $renyuan['one_str'] = '';

        $renyuan['three_str'] = '';

        $renyuan['last_person_price'] = '';

        if (!empty($order_goods)) {

            foreach ($order_goods as $k => &$v) {

                $v['three_level_id'] = $v['id'];

                $v['three_level_name'] = Db::name('goods_type')->where('id', $v['id'])->value('title');

                $price = Db::name('goods_type')->where('id', $v['id'])->value('price');

                $two_id = Db::name('goods_type')->where('id', $v['id'])->value('parenTid');

                $v['two_level_id'] = Db::name('goods_type')->where('id', $v['id'])->value('parenTid');

                $v['two_level_name'] = Db::name('goods_type')->where('id', $two_id)->value('title');

                $v['one_level_id'] = Db::name('goods_type')->where('id', $two_id)->value('parenTid');

                $three_id = Db::name('goods_type')->where('id', $two_id)->value('parenTid');

                $v['one_level_name'] = Db::name('goods_type')->where('id', $three_id)->value('title');

                $v['person_price'] = $price * $v['number'];

                $renyuan['one_str'] .= $v['one_level_name'] . ' ';

                $renyuan['three_str'] .= $v['three_level_name'] . ' ';

                $renyuan['last_person_price'] += $price * $v['number'];

            }

        }

        $service_price = Db::name('goods_type')->where('id', $id)->value('price');

        return $service_price*$order_goods[0]['number'];

    }



    /**

     * 商品和服务 退款消息提醒

     */

    public function refund_news()

    {

        $level = \session('role_id');//当前登录的管理员界别 5 7 8 9 10

        if($level == 5) {

            $refund_order_5 =

                Db::name('order')

                    ->whereOr('order_type', 1)

                    ->whereOr('order_type', 2)

                    ->where('shop_id',session('shop_id'))

                    ->where('status',10)

                    ->where('is_read_5',0)

                    ->find();//商品和服务



            $refund_order_55 =

                Db::name('order')

                    ->where('shop_id',0)

                    ->where('order_type',3)

                    ->where('status',10)

                    ->where('is_read_5',0)

                    ->find();

            if($refund_order_5){

                return ['code' => 200, 'message' => '查询到5未读商品或者服务'.Db::getlastSql().$level];

            }

            if($refund_order_55){

                //return ['code' => 200, 'message' => '查询到5未读礼包'.Db::getlastSql().$level];

            }

        }

        if($level == 7) {

            $str =

                Db::name('order')

                    ->whereOr('order_type', 1)

                    ->whereOr('order_type', 2)

                    ->where('is_read_5',1)->where('is_read_7',0)->column('id');

            $str = implode(',',$str);

            $refund_order_5 = Db::name('order')

                ->whereOr('order_type', 1)

                ->whereOr('order_type', 2)

                ->where('is_read_5',1)->where('is_read_7',0)->find();

            if($refund_order_5){

                return ['code' => 200, 'message' => '商品和服务 退款查询到7未读'.Db::getlastSql().$level.'/'.$refund_order_5['id'].'/id_str='.$str];

            }

        }

        if($level == 8) {

            $refund_order_5 = Db::name('order')

                ->whereOr('order_type', 1)

                ->whereOr('order_type', 2)

                ->where('is_read_7',1)->where('is_read_8',0)->find();

            if($refund_order_5){

                return ['code' => 200, 'message' => '商品和服务 退款查询到8未读'.Db::getlastSql().$level];

            }

        }

        if($level == 9) {

            $refund_order_5 = Db::name('order')

                ->whereOr('order_type', 1)

                ->whereOr('order_type', 2)

                ->where('is_read_8',1)->where('is_read_9',0)->find();

            if($refund_order_5){

                return ['code' => 200, 'message' => '商品和服务 退款查询到9未读'.Db::getlastSql().$level];

            }

        }

        if($level == 10) {

            $refund_order_5 = Db::name('order')

                ->whereOr('order_type', 1)

                ->whereOr('order_type', 2)

                ->where('is_read_9',1)->where('is_read_10',0)->find();

            if($refund_order_5){

                return ['code' => 200, 'message' => '商品和服务 退款查询到10未读'.Db::getlastSql().$level];

            }

        }

        return ['code' => 0, 'message' => '非管理类'];



    }



    /**

     * @return array

     * 退款单数量

     */

    public function refund_service_count()

    {

        $level = \session('role_id');//当前登录的管理员界别 5 7 8 9 10

        if($level == 5) {

            $count = Db::name('order')->where('shop_id',session('shop_id'))->where('order_type',2)->where('status',10)->where('is_read_5',0)->count();

            return ['code' => 200,'message' => '订单消息','count' => $count];

        }

        if($level == 7){

            $count = Db::name('order')->where('order_type',2)->where('status',10)->where('is_read_5',1)->where('is_read_7',0)->count();

            return ['code' => 200,'message' => '订单消息','count' => $count];

        }

        if($level == 8){

            $count = Db::name('order')->where('order_type',2)->where('status',10)->where('is_read_7',1)->where('is_read_8',0)->count();

            return ['code' => 200,'message' => '订单消息','count' => $count];

        }

        if($level == 9){

            $count = Db::name('order')->where('order_type',2)->where('status',10)->where('is_read_8',1)->where('is_read_9',0)->count();

            return ['code' => 200,'message' => '订单消息','count' => $count];

        }

        if($level == 10){

            $count = Db::name('order')->where('order_type',2)->where('status',10)->where('is_read_9',1)->where('is_read_10',0)->count();

            return ['code' => 200,'message' => '订单消息','count' => $count];

        }



    }

    public function refund_goods_count()

    {

        $level = \session('role_id');//当前登录的管理员界别 5 7 8 9 10

        if($level == 5) {

            $count = Db::name('order')->where('shop_id',session('shop_id'))->where('order_type',1)->where('status',10)->where('is_read_5',0)->count();

            return ['code' => 200,'message' => '商品消息','count' => $count];

        }

        if($level == 7){

            $count = Db::name('order')->where('order_type',1)->where('status',10)->where('is_read_5',1)->where('is_read_7',0)->count();

            return ['code' => 200,'message' => '商品消息','count' => $count];

        }

        if($level == 8){

            $count = Db::name('order')->where('order_type',1)->where('status',10)->where('is_read_7',1)->where('is_read_8',0)->count();

            return ['code' => 200,'message' => '商品消息','count' => $count];

        }

        if($level == 9){

            $count = Db::name('order')->where('order_type',1)->where('status',10)->where('is_read_8',1)->where('is_read_9',0)->count();

            return ['code' => 200,'message' => '商品消息','count' => $count];

        }

        if($level == 10){

            $count = Db::name('order')->where('order_type',1)->where('status',10)->where('is_read_9',1)->where('is_read_10',0)->count();

            return ['code' => 200,'message' => '商品消息','count' => $count];

        }



    }



    public function refund_gift_count()

    {

        $level = \session('role_id');//当前登录的管理员界别 5 7 8 9 10

//        if($level == 5) {

//            $count2 =

//                Db::name('order')

//                    ->where('order_type',3)

//                    ->where('status',10)

//                    ->where('is_read_5',0)

//                    ->count();

//            if($count2){

//                return ['code' => 200, 'message' => '查询到5未读礼包'.Db::getlastSql().$level,'count' =>$count2];

//            }

//        }

        //礼包退款取消 门店判断

        if($level == 7){ //副店长看门店是否提交退款了;

            $count = Db::name('order')->where('order_type',3)->where('status',10)->where('is_read_7',0)->count();

            return ['code' => 200,'message' => '礼包消息','count' => $count,'level' => $level];

        }

        if($level == 8){

            $count = Db::name('order')->where('order_type',3)->where('status',10)->where('is_read_7',1)->where('is_read_8',0)->count();

            return ['code' => 200,'message' => '礼包消息','count' => $count,'level' => $level];

        }

        if($level == 9){

            $count = Db::name('order')->where('order_type',3)->where('status',10)->where('is_read_8',1)->where('is_read_9',0)->count();

            return ['code' => 200,'message' => '礼包消息','count' => $count,'level' => $level];

        }

        if($level == 10){

            $count = Db::name('order')->where('order_type',3)->where('status',10)->where('is_read_9',1)->where('is_read_10',0)->count();

            return ['code' => 200,'message' => '礼包消息','count' => $count,'level' => $level];

        }



    }



    /**

     * @param $member_id

     * @param $order_number

     * 订单退款

     */

    public function refund($member_id, $order_number)

    {

        writeLogOrder($member_id . '/' . $order_number);

        $manage_id = Db::name('share_member')->where('member_id', $member_id)->value('manage_id');//获得直属业务员

        $salesman_array = parentPath($manage_id, 'salesman', true);//当前业务员上级

        $salesman_list = array_reverse($salesman_array);

        // $salesman_list = array_slice($salesman_array,0,3);



        if ($salesman_list) {

            $pre = 0;

            foreach ($salesman_list as $k => $v) {

                if ($k > 2) break;

                Db::name('member_distribution_records')->where('member_id', $member_id)->where('manage_id', $v['manage_id'])->where('order_number', $order_number)->delete();

            }

        }

    }



    public function news()

    {

        $time = date('Y-m-d H:i:s',time() - 36000);//五分钟

        $times = time()- 36000;//五分钟

       // $date = date('Y-m-d H:i:s', $time);

        $ret = Db::name('order')

            ->whereOr('order_type', 1)

            ->whereOr('order_type', 2)

            ->where('is_read',0)

            //->whereTime('create_time','>',$times)

            ->fetchSql(false)->find();

        //whereTime('create_time', '>=', $date)->

        if ($ret) {

            return ['code' => 200, 'message' => '查询到最新订单平台'.Db::getlastSql()];

        }

        return ['code' => 0, 'message' => '没有'];



    }



    public function service_count()

    {

        $count = Db::name('order')->where('order_type',2)->where('is_read',0)->count();

        return ['code' => 200,'message' => '订单消息','count' => $count];

    }

    public function goods_count()

    {

        $count = Db::name('order')->where('order_type',1)->where('is_read',0)->count();

        return ['code' => 200,'message' => '订单消息','count' => $count];

    }

    public function gift_count()

    {

        $count = Db::name('order')->where('order_type',3)->where('is_read',0)->count();

        return ['code' => 200,'message' => '订单消息gift_count','count' => $count];

    }





    /**

     * @return array商家订单提醒 只用于商品和服务订单

     */

    public function news_shop()

    {

        $time = time() - 600;//五分钟

        $date = date('Y-m-d H:i:s', $time);

        $auding_status = Session::get('auding_status');

        if($auding_status == 1){

            $where['shop_id'] = ['eq',\session('shop_id')];

        }

        $ret = Db::name('order')

            ->whereOr('order_type', 1)

            ->whereOr('order_type', 2)

            ->where('is_read_shop',0)

            ->where($where)

//            ->where('create_time','lt',$time)

            ->fetchSql(false)->find();

        //whereTime('create_time', '>=', $date)->

        if ($ret) {

            return ['code' => 200, 'message' => '查询到最新订单门店 提示音order_type 1 2 shop_id='.\session('shop_id').'order_id='.$ret['id']];

        }

        return ['code' => 0, 'message' => '没有'];



    }



    /**

     * 门店礼包消息提醒

     */

    public function news_gift()

    {

        //如果默认是全部未读

        $order = Db::name('order')->where('order_type', 3)->select()->toArray();

        $ret = false;//全部是已读消息 如果有一个未读就返回其他状态

        if (!empty($order)) {

            foreach ($order as $k => $v) {

                //按照管理员ID来 没有门店ID //发现一个空的就是没有查询大 就是未读

                $gift_news = Db::name('gift_news')->where('order_id', $v['id'])->where('manage_id',session('manage_id'))->find();

                //非空代表已读 空代表 未读过

                if(empty($gift_news)){ //包含有未读消息

                    $ret = true;

                }

            }

        }

        return $ret;

    }



    /**

     * @return int

     * 礼包消息提醒

     */

    public function news_gift_count()

    {

        $order = Db::name('order')->where('order_type', 3)->select()->toArray();

        $ret = 0;//默认没有未读消息0

        if (!empty($order)) {

            foreach ($order as $k => $v) {

                //按照管理员ID来 没有门店ID

                $gift_news = Db::name('gift_news')->where('order_id', $v['id'])->where('manage_id',session('manage_id'))->find();

                if(empty($gift_news)){ //包含有未读消息

                    $ret++;

                }

            }

        }

        return $ret;

    }



    /**

     * @return array

     * 门店未读订单

     */

    public function service_count_shop()

    {

        $where = [];

        $auding_status = Session::get('auding_status');

        if($auding_status == 1){

            $where['shop_id'] = ['eq',\session('shop_id')];

        }

        $count = Db::name('order')->where($where)->where('order_type',2)->where('is_read_shop',0)->count();

        return ['code' => 200,'message' => '订单消息','count' => $count];

    }

    public function goods_count_shop()

    {

        $where = [];

        $auding_status = Session::get('auding_status');

        if($auding_status == 1){

            $where['shop_id'] = ['eq',\session('shop_id')];

        }

        $count = Db::name('order')->where($where)->where('order_type',1)->where('is_read_shop',0)->count();

        return ['code' => 200,'message' => '订单消息','count' => $count];

    }

    public function gift_count_shop()

    {

        $where = [];

        $auding_status = Session::get('auding_status');

        if($auding_status == 1){

            $where['shop_id'] = ['eq',\session('shop_id')];

        }

        $count = Db::name('order')->where('order_type',3)->where('is_read_shop',0)->count();



        return ['code' => 200,'message' => '订单消息','count' => $count];

    }



    //礼包退款提示音

    public function refund_gift_news()

    {

        $level = \session('role_id');//当前登录的管理员界别 门店管理员 5 7 8 9 10

//        if($level == 5) {

//            $refund_order_5 =

//                Db::name('order')

//                    ->whereOr('order_type', 1)

//                    ->whereOr('order_type', 2)

//                    ->where('shop_id',session('shop_id'))

//                    ->where('status',10)

//                    ->where('is_read_5',0)

//                    ->find();//商品和服务

//            $refund_order_55 =

//                Db::name('order')

//                    ->where('shop_id',0)

//                    ->where('order_type',3)

//                    ->where('status',10)

//                    ->where('is_read_5',0)

//                    ->find();

//            if($refund_order_5){

//                return ['code' => 200, 'message' => '礼包退款提示音,查询到5未读商品或者服务'.Db::getlastSql().$level];

//            }

//            if($refund_order_55){

//                //return ['code' => 200, 'message' => '查询到5未读礼包'.Db::getlastSql().$level];

//            }

//        }

        if($level == 7) {

            $refund_order_5 = Db::name('order')

                ->where('order_type', 3)

                ->where('status',10)

                ->where('is_read_7',0)->find();

            $str = Db::name('order')

                ->where('order_type', 3)

                ->where('status',10)

                ->where('is_read_7',0)->column('id');

            if($refund_order_5){

                return ['code' => 200, 'message' => '礼包副经理查询到7未读'.Db::getlastSql().'@级别'.$level.'/总数'.count($str).'--'.implode(',',$str)];

            }

        }

        if($level == 8) {

            $refund_order_5 = Db::name('order')

                ->where('status',10)

                ->where('order_type', 3)

                ->where('is_read_7',1)->where('is_read_8',0)->find();

            if($refund_order_5){

                return ['code' => 200, 'message' => '礼包经理查询到8未读'.Db::getlastSql().$level.'/'.$refund_order_5['id']];

            }

        }

        if($level == 9) {

            $refund_order_5 = Db::name('order')

                ->where('status',10)

                ->where('order_type', 3)

                ->where('is_read_8',1)->where('is_read_9',0)->find();

            if($refund_order_5){

                return ['code' => 200, 'message' => '礼包财务查询到9未读'.Db::getlastSql().$level.'/'.$refund_order_5['id']];

            }

        }

        if($level == 10) {

            $refund_order_5 = Db::name('order')

                ->where('status',10)

                ->where('order_type', 9)

                ->where('is_read_9',1)->where('is_read_10',0)->find();

            if($refund_order_5){

                return ['code' => 200, 'message' => '礼包总经理查询到10未读'.Db::getlastSql().$level.'/'.$refund_order_5['id']];

            }

        }

        return ['code' => 0, 'message' => '非管理类'];



    }



    public function abc(){

        $order = Db::name('order')->where('order_number','201902011509015495810829')->fetchSql(true)->find();//订单所有数据

        halt($order);

    }



    /**

     * @param $id

     * @param $shop_id

     * @return array

     * 礼包

     */

    public function update_shop_id($id, $shop_id)

    {

        $ret = Db::name('order')->where('id', $id)->setField('shop_id', $shop_id);

        if ($ret !== false) {

            return ['code' => 200, 'message' => '门店修改成功'];

        }

        return ['code' => 400, 'message' => '失败'];



    }



    /**

     * @param $id

     * @param $shop_id

     * @return array

     * 修改商品门店id

     */

    public function goods_shop_id($id, $shop_id)

    {

        Db::startTrans();

        try {

            Db::name('order')->where('id', $id)->setField('shop_id', $shop_id);

            $sql = Db::name('order_goods')->where('order_id', $id)->select();

            foreach ($sql as $v) {

                Db::name('order_goods')->where('id', $v['id'])->setField('shop_id', $shop_id);

                $shop_where['shop_id'] = $shop_id;

                $shop_where['goods_id'] = $v['goods_id'];

                $shop_where['attribute'] = $v['attribute'];

                Db::name('shop_spec_attr')->where($shop_where)->setDec('inventory', $v['number']);//新门店

                $where['shop_id'] = $v['shop_id'];

                $where['goods_id'] = $v['goods_id'];

                $where['attribute'] = $v['attribute'];

                Db::name('shop_spec_attr')->where($where)->setInc('inventory', $v['number']); //老门店

                //Db::name('sale_number')->where('id',$v['goods_id'])->setDec('sale_number',$v['number']);

            }

            Db::commit();

            return ['code' => 200, 'message' => '门店修改成功'];

        } catch (\Exception $e) {

            Db::rollback();

            return ['code' => $e->getCode(), 'message' =>$e->getMessage()];

        }



    }



    /**

     * @param $id

     * 免费订单审核

     */

    public function auding_order($id, $auding_status,$reason)

    {



        $id = Request::instance()->param('id');

        $status = (Db::name('order')->where('id', $id)->value($auding_status) == '1' || Db::name('order')->where('id', $id)->value($auding_status) == '3' ) ? 2 : 3;



        $sql_auding_status1 = Db::name('order')->where('id', $id)->value('auding_status1');

        $sql_auding_status2 = Db::name('order')->where('id', $id)->value('auding_status2');

        if($status == 3){

            $find = Db::name('order')->where('id', $id)->update([$auding_status => $status,'reason' => $reason,'auding_status' => 3]);

        }else{

            $find = Db::name('order')->where('id', $id)->update([$auding_status => $status,'auding_status' => 1,'reason' => '']);

        }



        if ($find) {

            if ($auding_status == 'auding_status3' && $sql_auding_status1 == 2 && $sql_auding_status2 == 2) {

                Db::name('order')->where('id', $id)->setField('auding_status', '2');

            }

            return $status;

        }



    }



    /**

     * @param $id

     * @param $auding_status

     * @param $reason

     * @return int

     * 确认寄存力轮胎审核

     */

    public function auding_orderf($id, $auding_status,$reason)

    {



        $id = Request::instance()->param('id');

        $status = (Db::name('order')->where('id', $id)->value($auding_status) == '1' || Db::name('order')->where('id', $id)->value($auding_status) == '3' ) ? 2 : 3;

        $data = Db::name('order')->where('id',$id)->find();

//        halt($data);



        $sql_auding_status1 = Db::name('order')->where('id', $id)->value('auding_status1f');

        $sql_auding_status2 = Db::name('order')->where('id', $id)->value('auding_status2f');

        if($status == 3){

            $find = Db::name('order')->where('id', $id)->update([$auding_status => $status,'reasonf' => $reason,'auding_statusf' => 3]);

        }else{

            $find = Db::name('order')->where('id', $id)->update([$auding_status => $status,'auding_statusf' => 1,'reasonf' => '']);

        }



        if ($find) {

            if ($auding_status == 'auding_status3f' && $sql_auding_status1 == 2 && $sql_auding_status2 == 2) {

               $d =  Db::name('order')->where('id', $id)->setField('auding_statusf', '2');



                if($data['amount'] == 0){

                    $cont = '寄存方式:免费寄存';

                }else{

                    $cont = '寄存方式:付费寄存,金额'.number_format($data['amount'],2).'元';

                }

                Db::name('order')->where('id',$id)->setField('take_tyre',3);

                $member_id =Db::name('member')->where('username',$data['username'])->value('id');

                (new \app\index\controller\MemberMessage())->jpushMessage($member_id,'请确认轮胎是否已寄存,'.$cont,'寄存区间:'.$data['start_time'].'/'.$data['end_time'],3);



//                (new \app\index\controller\MemberMessage())->jpushMessage('请确认轮胎是否已寄存',35,5);

            }

            return $status;

        }



    }



    public function auding_orderq($id, $auding_status,$reason)

    {



        $id = Request::instance()->param('id');

        $status = (Db::name('order')->where('id', $id)->value($auding_status) == '1' || Db::name('order')->where('id', $id)->value($auding_status) == '3' ) ? 2 : 3;

        $data = Db::name('order')->where('id',$id)->find();

//        halt($data);

        $sql_auding_status1 = Db::name('order')->where('id', $id)->value('auding_status1q');

        $sql_auding_status2 = Db::name('order')->where('id', $id)->value('auding_status2q');

        if($status == 3){

            $find = Db::name('order')->where('id', $id)->update([$auding_status => $status,'reasonq' => $reason,'auding_statusq' => 3]);

        }else{

            $find = Db::name('order')->where('id', $id)->update([$auding_status => $status,'auding_statusq' => 1,'reasonq' => '']);

        }



        if ($find) {

            if ($auding_status == 'auding_status3q' && $sql_auding_status1 == 2 && $sql_auding_status2 == 2) {

                Db::name('order')->where('id', $id)->setField('auding_statusq', '2');



                if($data['amount'] == 0){

                    $cont = '寄存方式:免费寄存';

                }else{

                    $cont = '寄存方式:付费寄存,金额'.number_format($data['amount'],2).'元';

                }

                Db::name('order')->where('id',$id)->setField('take_tyre',4);

                $member_id =Db::name('member')->where('username',$data['username'])->value('id');

                (new \app\index\controller\MemberMessage())->jpushMessage($member_id,'请确认轮胎是否已取走,'.$cont,'寄存区间:'.$data['start_time'].'/'.$data['end_time'],3);



//                (new \app\index\controller\MemberMessage())->jpushMessage('请确认轮胎是否已寄存',35,5);

            }

            return $status;

        }



    }



    public function single_explain($id,$single_explain){

        $find = Db::name('order')->where('id', $id)->update(['single_explain' => $single_explain,]);

        return 2;

    }









}